home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / ld / ldfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-13  |  8.2 KB  |  380 lines

  1. /* Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc.
  2.  
  3. This file is part of GLD, the Gnu Linker.
  4.  
  5. GLD is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 1, or (at your option)
  8. any later version.
  9.  
  10. GLD is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GLD; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /*
  20.  ldfile.c
  21.  
  22.  look after all the file stuff
  23.  
  24.  */
  25.  
  26. #include "bfd.h"
  27. #include "sysdep.h"
  28. #include "ld.h"
  29. #include "ldmisc.h"
  30. #include "ldexp.h"
  31. #include "ldlang.h"
  32. #include "ldfile.h"
  33. #include "ldmain.h"
  34. #include "ldgram.h"
  35. #include "ldlex.h"
  36. #include "ldemul.h"
  37.  
  38. #include <ctype.h>
  39.  
  40. const char *ldfile_input_filename;
  41. boolean ldfile_assumed_script = false;
  42. const char *ldfile_output_machine_name = "";
  43. unsigned long ldfile_output_machine;
  44. enum bfd_architecture ldfile_output_architecture;
  45. search_dirs_type *search_head;
  46.  
  47. #ifdef VMS
  48. char *slash = "";
  49. #else
  50. char *slash = "/";
  51. #endif
  52.  
  53. /* LOCAL */
  54.  
  55. static search_dirs_type **search_tail_ptr = &search_head;
  56.  
  57. typedef struct search_arch 
  58. {
  59.   char *name; 
  60.   struct search_arch *next;
  61. } search_arch_type;
  62.  
  63. static search_arch_type *search_arch_head;
  64. static search_arch_type **search_arch_tail_ptr = &search_arch_head;
  65.  
  66. static boolean try_open_bfd PARAMS ((const char *attempt,
  67.                      lang_input_statement_type *entry));
  68. static FILE *try_open PARAMS ((const char *name, const char *exten));
  69.  
  70. void
  71. ldfile_add_library_path (name, cmdline)
  72.      const char *name;
  73.      boolean cmdline;
  74. {
  75.   search_dirs_type *new;
  76.  
  77.   new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
  78.   new->next = NULL;
  79.   new->name = name;
  80.   new->cmdline = cmdline;
  81.   *search_tail_ptr = new;
  82.   search_tail_ptr = &new->next;
  83. }
  84.  
  85. /* Try to open a BFD for a lang_input_statement.  */
  86.  
  87. static boolean
  88. try_open_bfd (attempt, entry)
  89.      const char *attempt;
  90.      lang_input_statement_type *entry;
  91. {
  92.   entry->the_bfd = bfd_openr (attempt, entry->target);
  93.  
  94.   if (trace_file_tries)
  95.     info_msg ("attempt to open %s %s\n", attempt,
  96.           entry->the_bfd == NULL ? "failed" : "succeeded");
  97.  
  98.   if (entry->the_bfd != NULL)
  99.     return true;
  100.   else
  101.     return false;
  102. }
  103.  
  104. /* Search for and open the file specified by ENTRY.  If it is an
  105.    archive, use ARCH, LIB and SUFFIX to modify the file name.  */
  106.  
  107. boolean
  108. ldfile_open_file_search (arch, entry, lib, suffix)
  109.      const char *arch;
  110.      lang_input_statement_type *entry;
  111.      const char *lib;
  112.      const char *suffix;
  113. {
  114.   search_dirs_type *search;
  115.  
  116.   /* If this is not an archive, try to open it in the current
  117.      directory first.  */
  118.   if (! entry->is_archive)
  119.     {
  120.       if (try_open_bfd (entry->filename, entry))
  121.     return true;
  122.     }
  123.  
  124.   for (search = search_head;
  125.        search != (search_dirs_type *)NULL;
  126.        search = search->next) 
  127.     {
  128.       char *string;
  129.  
  130.       string = (char *) xmalloc (strlen (search->name)
  131.                  + strlen (slash)
  132.                  + strlen (lib)
  133.                  + strlen (entry->filename)
  134.                  + strlen (arch)
  135.                  + strlen (suffix)
  136.                  + 1);
  137.  
  138.       if (entry->is_archive)
  139.     sprintf (string, "%s%s%s%s%s%s", search->name, slash,
  140.          lib, entry->filename, arch, suffix);
  141.       else if (entry->filename[0] == '/' || entry->filename[0] == '.')
  142.     strcpy (string, entry->filename);
  143.       else
  144.     sprintf (string, "%s%s%s", search->name, slash, entry->filename);
  145.  
  146.       if (try_open_bfd (string, entry))
  147.     {
  148.       entry->filename = string;
  149.       return true;
  150.     }
  151.  
  152.       free (string);
  153.     }
  154.  
  155.   return false;
  156. }
  157.  
  158. /* Open the input file specified by ENTRY.  */
  159.  
  160. void
  161. ldfile_open_file (entry)
  162.      lang_input_statement_type *entry;
  163. {
  164.   if (entry->the_bfd != NULL)
  165.     return;
  166.  
  167.   if (! entry->search_dirs_flag)
  168.     {
  169.       if (try_open_bfd (entry->filename, entry))
  170.     return;
  171.     }
  172.   else
  173.     {
  174.       search_arch_type *arch;
  175.  
  176.       /* Try to open <filename><suffix> or lib<filename><suffix>.a */
  177.       for (arch = search_arch_head;
  178.        arch != (search_arch_type *) NULL;
  179.        arch = arch->next)
  180.     {
  181.       if (config.dynamic_link)
  182.         {
  183.           if (ldemul_open_dynamic_archive (arch->name, entry))
  184.         return;
  185.         }
  186.       if (ldfile_open_file_search (arch->name, entry, "lib", ".a"))
  187.         return;
  188. #ifdef VMS
  189.       if (ldfile_open_file_search (arch->name, entry, ":lib", ".a"))
  190.         return;
  191. #endif
  192.     }
  193.     }
  194.  
  195.   einfo("%F%P: cannot open %s: %E\n", entry->local_sym_name);
  196. }
  197.  
  198. /* Try to open NAME; if that fails, try NAME with EXTEN appended to it.  */
  199.  
  200. static FILE *
  201. try_open (name, exten)
  202.      const char *name;
  203.      const char *exten;
  204. {
  205.   FILE *result;
  206.   char buff[1000];
  207.  
  208.   result = fopen(name, "r");
  209.   if (trace_file_tries == true) {
  210.     if (result == (FILE *)NULL) {
  211.       info_msg ("cannot find ");
  212.     }
  213.     info_msg ("%s\n",name);
  214.   }
  215.   if (result != (FILE *)NULL) {
  216.     return result;
  217.   }
  218.  
  219.   if (*exten) {
  220.     sprintf(buff, "%s%s", name, exten);
  221.     result = fopen(buff, "r");
  222.     if (trace_file_tries == true) {
  223.       if (result == (FILE *)NULL) {
  224.     info_msg ("cannot find ");
  225.       }
  226.       info_msg ("%s\n", buff);
  227.     }
  228.   }
  229.   return result;
  230. }
  231.  
  232. /* Try to open NAME; if that fails, look for it in any directories
  233.    specified with -L, without and with EXTEND apppended.  */
  234.  
  235. FILE *
  236. ldfile_find_command_file (name, extend)
  237.      const char *name;
  238.      const char *extend;
  239. {
  240.   search_dirs_type *search;
  241.   FILE *result;
  242.   char buffer[1000];
  243.  
  244.   /* First try raw name */
  245.   result = try_open(name,"");
  246.   if (result == (FILE *)NULL) {
  247.     /* Try now prefixes */
  248.     for (search = search_head;
  249.      search != (search_dirs_type *)NULL;
  250.      search = search->next) {
  251.       sprintf(buffer,"%s/%s", search->name, name);
  252.       result = try_open(buffer, extend);
  253.       if (result)break;
  254.     }
  255.   }
  256.   return result;
  257. }
  258.  
  259. void
  260. ldfile_open_command_file (name)
  261.      const char *name;
  262. {
  263.   FILE *ldlex_input_stack;
  264.   ldlex_input_stack = ldfile_find_command_file(name, "");
  265.  
  266.   if (ldlex_input_stack == (FILE *)NULL) {
  267.     bfd_set_error (bfd_error_system_call);
  268.     einfo("%P%F: cannot open linker script file %s: %E\n",name);
  269.   }
  270.   lex_push_file(ldlex_input_stack, name);
  271.   
  272.   ldfile_input_filename = name;
  273.   lineno = 1;
  274.   had_script = true;
  275. }
  276.  
  277.  
  278.  
  279.  
  280.  
  281. #ifdef GNU960
  282. static
  283. char *
  284. gnu960_map_archname( name )
  285. char *name;
  286. {
  287.   struct tabentry { char *cmd_switch; char *arch; };
  288.   static struct tabentry arch_tab[] = {
  289.     "",   "",
  290.     "KA", "ka",
  291.     "KB", "kb",
  292.     "KC", "mc",    /* Synonym for MC */
  293.     "MC", "mc",
  294.     "CA", "ca",
  295.     "SA", "ka",    /* Functionally equivalent to KA */
  296.     "SB", "kb",    /* Functionally equivalent to KB */
  297.     NULL, ""
  298.   };
  299.   struct tabentry *tp;
  300.   
  301.  
  302.   for ( tp = arch_tab; tp->cmd_switch != NULL; tp++ ){
  303.     if ( !strcmp(name,tp->cmd_switch) ){
  304.       break;
  305.     }
  306.   }
  307.  
  308.   if ( tp->cmd_switch == NULL ){
  309.     einfo("%P%F: unknown architecture: %s\n",name);
  310.   }
  311.   return tp->arch;
  312. }
  313.  
  314.  
  315.  
  316. void
  317. ldfile_add_arch(name)
  318. char *name;
  319. {
  320.   search_arch_type *new =
  321.     (search_arch_type *)xmalloc((bfd_size_type)(sizeof(search_arch_type)));
  322.  
  323.  
  324.   if (*name != '\0') {
  325.     if (ldfile_output_machine_name[0] != '\0') {
  326.       einfo("%P%F: target architecture respecified\n");
  327.       return;
  328.     }
  329.     ldfile_output_machine_name = name;
  330.   }
  331.  
  332.   new->next = (search_arch_type*)NULL;
  333.   new->name = gnu960_map_archname( name );
  334.   *search_arch_tail_ptr = new;
  335.   search_arch_tail_ptr = &new->next;
  336.  
  337. }
  338.  
  339. #else    /* not GNU960 */
  340.  
  341.  
  342. void
  343. ldfile_add_arch (in_name)
  344.      CONST char * in_name;
  345. {
  346.   char *name = buystring(in_name);
  347.   search_arch_type *new =
  348.     (search_arch_type *)xmalloc((bfd_size_type)(sizeof(search_arch_type)));
  349.  
  350.   ldfile_output_machine_name = in_name;
  351.  
  352.   new->name = name;
  353.   new->next = (search_arch_type*)NULL;
  354.   while (*name) {
  355.     if (isupper(*name)) *name = tolower(*name);
  356.     name++;
  357.   }
  358.   *search_arch_tail_ptr = new;
  359.   search_arch_tail_ptr = &new->next;
  360.  
  361. }
  362. #endif
  363.  
  364. /* Set the output architecture */
  365. void
  366. ldfile_set_output_arch (string)
  367.      CONST char *string;
  368. {
  369.   bfd_arch_info_type *arch = bfd_scan_arch(string);
  370.  
  371.   if (arch) {
  372.     ldfile_output_architecture = arch->arch;
  373.     ldfile_output_machine = arch->mach;
  374.     ldfile_output_machine_name = arch->printable_name;
  375.   }
  376.   else {
  377.     einfo("%P%F: cannot represent machine `%s'\n", string);
  378.   }
  379. }
  380.